package com.easybuy.web.controller.manager;

import com.easybuy.bean.EasybuyNew;
import com.easybuy.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/news")
public class NewsController {

    @Autowired
    INewsService iNewsService;

    @RequestMapping("/listByPage")
    public ModelAndView listByPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        //2.调用service的分页方法
        List<EasybuyNew> newses = iNewsService.listByPage(page, pageSize);
        //3.构造modelAndView
        ModelAndView mv = new ModelAndView();
        //数据
        mv.addObject("newses", newses);
        mv.addObject("newsPage", page);
        mv.addObject("maxPage",(int)Math.ceil(iNewsService.outAll()/(double)pageSize));
        //视图
        mv.setViewName("/manage/news");
        System.out.println("开始删除");
        return mv;
    }

    @RequestMapping("/to_save")
    public String toSave() {
        return "manage/news-add";
    }

    @RequestMapping("/save_commit")
    public String saveCommit(EasybuyNew ebNews) {
        //1.获取表单的值--参数 参数校验
        if (ebNews.getEnTitle() == null || ebNews.getEnTitle().trim().length() == 0) {
            return "manage/paramError";
            //throw new EbExection();

        }

        //2.调service保存的方法
        int result = iNewsService.save(ebNews);
        if (result <= 0) {
            //TODO 要么return 要么跑出异常
        }
        return "manage/manage-result";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        System.out.println("开始删除");
        int result=iNewsService.deleteByPk(id);
        if(result==0){
            System.out.println("能删除");
        }
        System.out.println("删除后跳转");
        return "/manage/manage-result";
    }
    @RequestMapping("/edit")
    public String edit(Model model, Integer enId){
        //1.根据新闻的编号查询新闻的详细信息
        EasybuyNew easybuyNew = iNewsService.selectByCondition(enId);
        System.out.println(enId+"  "+easybuyNew);
        //2.将查出来的新闻信息传递给news-modify.jsp

        //model.addAttribute(s:"ebNews", easybuyNew);
        model.addAttribute("ebNews", easybuyNew);
        return "manage/news-modify";
    }
    @RequestMapping("/editSubmit")
    public String editSubmit(HttpServletRequest request,@RequestParam(value = "enId") Integer enId){
        EasybuyNew easybuyNew=new EasybuyNew();
        easybuyNew.setEnId(enId);
        easybuyNew.setEnTitle(request.getParameter("title"));
        easybuyNew.setEnContent(request.getParameter("content"));
        //1.更新新闻信息
        System.out.println(easybuyNew);
        int result = iNewsService.updateById(easybuyNew);
        //2.跳转到更新结果页面
        if (result <= 0){
            System.out.println("能修改");
            return "manage/error";
        }
        return "manage/manage-result";
    }
}
